{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Add Digits"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #math #number-theory #simulation"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数学 #数论 #模拟"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: addDigits"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #各位相加"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给定一个非负整数 <code>num</code>，反复将各个位上的数字相加，直到结果为一位数。返回这个结果。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1:</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入:</strong> num =<strong> </strong><code>38</code>\n",
    "<strong>输出:</strong> 2 \n",
    "<strong>解释: </strong>各位相加的过程为<strong>：\n",
    "</strong>38 --&gt; 3 + 8 --&gt; 11\n",
    "11 --&gt; 1 + 1 --&gt; 2\n",
    "由于&nbsp;<code>2</code> 是一位数，所以返回 2。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2:</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入:</strong> num =<strong> </strong>0\n",
    "<strong>输出:</strong> 0</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>0 &lt;= num &lt;= 2<sup>31</sup>&nbsp;- 1</code></li>\n",
    "</ul>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>进阶：</strong>你可以不使用循环或者递归，在 <code>O(1)</code> 时间复杂度内解决这个问题吗？</p>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [add-digits](https://leetcode.cn/problems/add-digits/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [add-digits](https://leetcode.cn/problems/add-digits/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['38', '0']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addDigits(self, num: int) -> int:\n",
    "        while True:\n",
    "            sum = 0\n",
    "            while num!=0:\n",
    "                a = num%10\n",
    "                num = num // 10\n",
    "                sum = sum + a\n",
    "            if sum<10:\n",
    "                return sum\n",
    "                break\n",
    "            else:\n",
    "                num = sum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addDigits(self, num: int) -> int:\n",
    "        result=num\n",
    "        while result >= 10:\n",
    "            result=0\n",
    "            while num >= 10:\n",
    "                result+=num%10\n",
    "                num//=10\n",
    "            else:\n",
    "                result+=num\n",
    "                if result>=10:\n",
    "                    num=result\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addDigits(self, num: int) -> int:\n",
    "        nums=str(num)\n",
    "        n=0\n",
    "        for i in nums:\n",
    "            n+=int(i)\n",
    "            if n >= 10:\n",
    "                n=n%10+1\n",
    "        return n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addDigits(self, num: int) -> int:\n",
    "        return (num -1) % 9 +1 if num else 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addDigits(self, num: int) -> int:\n",
    "        while num>=10:\n",
    "\n",
    "            a = num%10 \n",
    "            b = num//10\n",
    "            num = a+b\n",
    "        return num\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addDigits(self, num: int) -> int:\n",
    "        while num >= 10:\n",
    "            num = num // 10 + num % 10\n",
    "        return num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "'''\n",
    "class Solution:\n",
    "    def addDigits(self, num: int) -> int:\n",
    "        sum = 0\n",
    "        for ele in str(num):\n",
    "            sum = sum + int(ele)\n",
    "        if sum >= 10:\n",
    "            return self.addDigits(sum)\n",
    "        else:\n",
    "            return sum\n",
    "'''\n",
    "'''\n",
    "class Solution:\n",
    "    def addDigits(self, num: int) -> int:\n",
    "        total_sum = 0\n",
    "        for digit in str(num):\n",
    "            total_sum += int(digit)\n",
    "        if total_sum >= 10:\n",
    "            return self.addDigits(total_sum)\n",
    "        else:\n",
    "            return total_sum\n",
    "'''\n",
    "'''\n",
    "class Solution:\n",
    "    def addDigits(self, num:int) -> int:\n",
    "'''\n",
    "\n",
    "class Solution:\n",
    "    def addDigits(self, num: int) -> int:\n",
    "        while num >= 10:\n",
    "            sum = 0\n",
    "            while num:\n",
    "                sum += num % 10\n",
    "                num //= 10\n",
    "            num = sum\n",
    "        return num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addDigits(self, num: int) -> int:\n",
    "        if num < 10:\n",
    "            return num\n",
    "        ans = 0\n",
    "        while num > 0:\n",
    "            ans += num % 10\n",
    "            num //= 10\n",
    "        return self.addDigits(ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addDigits(self, num: int) -> int:\n",
    "        while num >= 10:\n",
    "            n = num\n",
    "            tmp = 0\n",
    "            while n > 0:\n",
    "                tmp += n % 10\n",
    "                n //= 10\n",
    "            num = tmp \n",
    "        return num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addDigits(self, num: int) -> int:\n",
    "        sum = 0\n",
    "        num_str = str(num)\n",
    "        for i in num_str:\n",
    "            sum = sum+int(i)\n",
    "        if len(str(sum))==1:\n",
    "            return sum\n",
    "        else:\n",
    "            return self.addDigits(sum)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addDigits(self, num: int) -> int:\n",
    "        if num == 0:\n",
    "            return 0\n",
    "        while True:\n",
    "            reminder = 0\n",
    "            while num > 0:\n",
    "                reminder += num % 10 \n",
    "                num = num // 10\n",
    "            if reminder // 10 ==0:\n",
    "                return reminder\n",
    "            else:\n",
    "                num = reminder"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addDigits(self, num: int) -> int:\n",
    "        if num == 0:\n",
    "            return 0\n",
    "        if num %9 == 0:\n",
    "            return 9\n",
    "        else:\n",
    "            return num%9"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addDigits(self, num: int) -> int:\n",
    "        if num==0:\n",
    "            return 0\n",
    "        if num%9==0:\n",
    "            return 9\n",
    "        return num%9"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addDigits(self, num: int) -> int:\n",
    "        if num == 0:\n",
    "            return 0\n",
    "        elif num % 9 == 0:\n",
    "            return 9\n",
    "        else:\n",
    "            return num % 9"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addDigits(self, num: int) -> int:\n",
    "        while num >= 10:\n",
    "            su = 0\n",
    "            while num:\n",
    "                su += num % 10\n",
    "                num //= 10\n",
    "            num = su\n",
    "        return num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addDigits(self, num: int) -> int:\n",
    "        if (num)<10:\n",
    "            return num\n",
    "        while 1:\n",
    "            theSum = num//10 + num%10\n",
    "            if theSum > 9:\n",
    "                num = num//10 + num%10\n",
    "            else:\n",
    "                return theSum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addDigits(self, num: int) -> int:\n",
    "        if num == 0:\n",
    "            return 0\n",
    "        res = num % 9\n",
    "        if res == 0:\n",
    "            res = 9\n",
    "        return res\n",
    "\n",
    "        p = str(num)\n",
    "        while len(p) > 1:\n",
    "            pn = sum([int(i) for i in p])\n",
    "            p = str(pn)\n",
    "        return int(p)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addDigits(self, num: int) -> int:\n",
    "        while num >= 10:\n",
    "            num = sum(map(int, str(num)))\n",
    "        return num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addDigits(self, num: int) -> int:\n",
    "\n",
    "\n",
    "        # 拆分相加函数\n",
    "        def helper(n):\n",
    "            # list = [] \n",
    "            # while n > 0:\n",
    "            #     cur = n % 10\n",
    "            #     list.append(cur)\n",
    "            #     n //= 10\n",
    "            # return sum(list)\n",
    "        # print(helper(num))\n",
    "            sum = 0\n",
    "            while n > 0:\n",
    "                sum += n % 10\n",
    "                n //= 10\n",
    "            return sum\n",
    "\n",
    "        while num >= 10:\n",
    "            num = helper(num)\n",
    "        return num\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addDigits(self, num: int) -> int:\n",
    "        while num >= 10:\n",
    "            sum = 0\n",
    "            while num:\n",
    "                sum += num % 10\n",
    "                num //= 10\n",
    "            num = sum\n",
    "        return num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addDigits(self, num: int) -> int:\n",
    "        count = 9999999999\n",
    "        while count > 0 and num >= 10:\n",
    "            for i in range(len(str(num))-1):\n",
    "                num = sum(int(char) for char in str(num))\n",
    "                count -= 1\n",
    "\n",
    "        return num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addDigits(self, num: int) -> int:   \n",
    "        c = 0\n",
    "        while (num != 0):\n",
    "            b = num % 10\n",
    "            c = c + b\n",
    "            num = int (num / 10)\n",
    "            if (c > 9):\n",
    "                num += c\n",
    "                c = 0\n",
    "            if (c < 10 and num == 0):\n",
    "                break\n",
    "        return c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addDigits(self, num: int) -> int:\n",
    "        if num < 10:\n",
    "            return num\n",
    "        s = 0\n",
    "        while num:\n",
    "            s += num % 10\n",
    "            num //= 10\n",
    "        return self.addDigits(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addDigits(self, num: int) -> int:\n",
    "        while num >= 10:\n",
    "            res = 0\n",
    "            for i in str(num):\n",
    "                res += int(i)\n",
    "            num = res\n",
    "        return num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addDigits(self, num: int) -> int:\n",
    "        return 1 + (num - 1) % 9 if num else 0\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addDigits(self, num: int) -> int:\n",
    "        s=str(num)\n",
    "        while len(s)!=1:\n",
    "            s=str(sum(int(i) for i in s))\n",
    "        return int(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addDigits(self, num: int) -> int:\n",
    "        while num > 9:\n",
    "            num = sum(list(map(int, list(str(num)))))\n",
    "        return num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addDigits(self, num: int) -> int:\n",
    "        sum = 0\n",
    "        while(num>0):\n",
    "            sum+=num%10\n",
    "            num=int(num/10)\n",
    "        if sum < 10:\n",
    "            return sum\n",
    "        else:\n",
    "            return self.addDigits(sum)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addDigits(self, num: int) -> int:\n",
    "        if num == 0:\n",
    "            return 0\n",
    "        while num > 9:\n",
    "            s = str(num)\n",
    "            num = 0\n",
    "            for i in s:\n",
    "                num += int(i)\n",
    "        else:\n",
    "            return num\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addDigits(self, num: int) -> int:\n",
    "        p = str(num)\n",
    "        while len(p) > 1:\n",
    "            pn = sum([int(i) for i in p])\n",
    "            p = str(pn)\n",
    "        return int(p)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addDigits(self, num: int) -> int:\n",
    "        x=num%10+num//10\n",
    "        while x>=10:\n",
    "            x=x%10+x//10\n",
    "        return x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addDigits(self, num: int) -> int:\n",
    "        def Extractdigit(num):\n",
    "            ans=0\n",
    "            while num > 0:\n",
    "                digit = num % 10\n",
    "                num = num // 10\n",
    "                ans+=digit\n",
    "            return ans\n",
    "\n",
    "        if num<10:\n",
    "            return num\n",
    "        else:\n",
    "            while num >= 10:\n",
    "                num=Extractdigit(num)\n",
    "            return num\n",
    "\n",
    "\n",
    "            \n",
    "        \n",
    "\n",
    "       \n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addDigits(self, num: int) -> int:\n",
    "        while num >= 10:\n",
    "            sum = 0\n",
    "            while num:\n",
    "                sum += num % 10\n",
    "                num //= 10\n",
    "            num = sum\n",
    "        return num\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addDigits(self, num: int) -> int:\n",
    "        if num==0:\n",
    "            return 0\n",
    "        return 9 if num%9==0 else num%9"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addDigits(self, num: int) -> int:\n",
    "        s=0\n",
    "        while num>=10:\n",
    "            b=int(num%10)\n",
    "            s+=b\n",
    "            num=int(num/10)\n",
    "        while num<10:\n",
    "            s+=num\n",
    "            while(s>=10):\n",
    "                s=int(s/10)+s%10\n",
    "            return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addDigits(self, num: int) -> int:\n",
    "        if num == 0:\n",
    "            return 0\n",
    "\n",
    "        while len(str(num)) != 1:\n",
    "            new_num = 0\n",
    "            for i in str(num):\n",
    "                new_num += int(i)\n",
    "\n",
    "            num = new_num\n",
    "\n",
    "        return num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addDigits(self, num: int) -> int:\n",
    "        while   num >= 10 :\n",
    "            a = num % 10\n",
    "            b = num  // 10\n",
    "            num = a + b\n",
    "        return num\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addDigits(self, num: int) -> int:\n",
    "        while num>=10:\n",
    "            sum=0\n",
    "            while num:\n",
    "                sum+=num%10\n",
    "                num//=10\n",
    "            num=sum\n",
    "        return num\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addDigits(self, num: int) -> int:\n",
    "      return 9 if (num % 9 == 0) and (num != 0) else num % 9"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addDigits(self, num: int) -> int:\n",
    "        while True:\n",
    "            if num//10==0:\n",
    "                return num\n",
    "            num=sum(int(i) for i in list(str(num)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addDigits(self, num: int) -> int:\n",
    "        if num < 10:\n",
    "            return num\n",
    "        def add(x):\n",
    "            res = 0\n",
    "            while x > 0:\n",
    "                res += x % 10\n",
    "                x //= 10\n",
    "            return res\n",
    "\n",
    "        while num >= 10:\n",
    "            if num % 10 == 0:\n",
    "                num //= 10\n",
    "            else:\n",
    "                num = add(num)\n",
    "        return num\n",
    "            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addDigits(self, num: int) -> int:\n",
    "        count = num\n",
    "        while len(str(count)) > 1:\n",
    "            num = count\n",
    "            count = 0\n",
    "            while num > 0:\n",
    "                count += num % 10\n",
    "                num //= 10\n",
    "        return count\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addDigits(self, num: int) -> int:\n",
    "        while len(str(num)) != 1:\n",
    "            l = [0 for i in range(0, len(str(num)))]\n",
    "            for i in range(0, len(str(num))):\n",
    "                l[i] = int(str(num)[i])\n",
    "            num = sum(l)\n",
    "        return num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addDigits(self, num: int) -> int:\n",
    "        while num > 9:\n",
    "            s = str(num)\n",
    "            num = 0\n",
    "            for i in s:\n",
    "                num += int(i)\n",
    "        else:\n",
    "            return num\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addDigits(self, num: int) -> int:\n",
    "        if num < 10:\n",
    "            return num\n",
    "        \n",
    "        _num = 0\n",
    "        while num > 0:\n",
    "            _num += num % 10\n",
    "            num //= 10\n",
    "        return self.addDigits(_num)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addDigits(self, num: int) -> int:\n",
    "        while num >= 10:\n",
    "            l = list(str(num))\n",
    "            num = 0\n",
    "            for i in l:\n",
    "                num += int(i)\n",
    "        return num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addDigits(self, num: int) -> int:\n",
    "        while num >= 10:\n",
    "            sum = 0\n",
    "            while num:\n",
    "                sum += num % 10\n",
    "                num //= 10\n",
    "            num = sum\n",
    "        return num\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addDigits(self, num: int) -> int:\n",
    "        x=num%10+num//10\n",
    "        while x>=10:\n",
    "            x=x%10+x//10\n",
    "        return x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addDigits(self, num: int) -> int:\n",
    "        \n",
    "       \n",
    "        while True:\n",
    "            temp=0\n",
    "            while (num>0):\n",
    "                temp=temp+num%10\n",
    "                num=num//10\n",
    "            if temp>=10:\n",
    "                num=temp\n",
    "            else:\n",
    "                return temp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addDigits(self, num: int) -> int:\n",
    "        # 模拟\n",
    "        if num == 0:\n",
    "            return 0\n",
    "            \n",
    "        def helper(num):\n",
    "            res = 0\n",
    "            while num:\n",
    "                x = num % 10  # 取得最后一位数\n",
    "                res += x\n",
    "                num //= 10\n",
    "            return res\n",
    "        while num:\n",
    "            num = helper(num)\n",
    "            if 0 < num < 10:\n",
    "                return num\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def addDigits(self, num: int) -> int:\r\n",
    "        # if num < 10:\r\n",
    "        #     return num\r\n",
    "        # s = str(num)\r\n",
    "        # res = 0\r\n",
    "        # for c in s: res += int(c)\r\n",
    "        # return self.addDigits(res)\r\n",
    "        # while num >= 10:\r\n",
    "        #     sum = 0\r\n",
    "        #     while num:\r\n",
    "        #         sum += num % 10\r\n",
    "        #         num //= 10\r\n",
    "        #     num = sum\r\n",
    "        # return num\r\n",
    "        return (num - 1) % 9 + 1 if num else 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addDigits(self, num: int) -> int:\n",
    "        if num // 10 == 0:\n",
    "            return num\n",
    "        \n",
    "        total = num % 10\n",
    "        while num // 10 > 0:\n",
    "            num //= 10\n",
    "            total += (num % 10)\n",
    "        \n",
    "        return self.addDigits(total)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addDigits(self, num: int) -> int:\n",
    "        while num >= 10:\n",
    "            sum = 0\n",
    "            while num:\n",
    "                sum += num % 10\n",
    "                num //= 10\n",
    "            num = sum\n",
    "        return num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addDigits(self, num: int) -> int:\n",
    "        def jia(num):\n",
    "            sum = 0\n",
    "            while num>0:\n",
    "                sum += num%10\n",
    "                num = num//10\n",
    "            return sum\n",
    "        while num>9:\n",
    "            num = jia(num)\n",
    "        return num\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addDigits(self, num: int) -> int:\n",
    "        def w(n):\n",
    "            res = 0\n",
    "            while n:\n",
    "                res += n % 10\n",
    "                n //= 10\n",
    "            return res\n",
    "        def rs(n):\n",
    "            if len(str(n)) == 1:\n",
    "                return n\n",
    "            return rs(w(n))\n",
    "        return rs(num)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def addDigits(self, num: int) -> int:\r\n",
    "        # if num < 10:\r\n",
    "        #     return num\r\n",
    "        # s = str(num)\r\n",
    "        # res = 0\r\n",
    "        # for c in s: res += int(c)\r\n",
    "        # return self.addDigits(res)\r\n",
    "        while num >= 10:\r\n",
    "            sum = 0\r\n",
    "            while num:\r\n",
    "                sum += num % 10\r\n",
    "                num //= 10\r\n",
    "            num = sum\r\n",
    "        return num\r\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addDigits(self, num: int) -> int:\n",
    "        while num >= 10:\n",
    "            sum = 0\n",
    "            while num:\n",
    "                sum += num % 10\n",
    "                num //= 10\n",
    "            num = sum\n",
    "        return num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addDigits(self, num: int) -> int:\n",
    "        while num >= 10:\n",
    "            num = sum(map(int, str(num)))\n",
    "        return num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addDigits(self, num: int) -> int:\n",
    "        while   num >= 10 :\n",
    "            a = num % 10\n",
    "            b = num  // 10\n",
    "            num = a + b\n",
    "        return num\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addDigits(self, num: int) -> int:\n",
    "        while num >= 10:\n",
    "            sum = 0\n",
    "            while num:\n",
    "                sum += num % 10\n",
    "                num //= 10\n",
    "            num = sum\n",
    "        return num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addDigits(self, num: int) -> int:\n",
    "        if num // 10 == 0:\n",
    "            return num\n",
    "        \n",
    "        total = num % 10\n",
    "        while num // 10 > 0:\n",
    "            num //= 10\n",
    "            total += (num % 10)\n",
    "        \n",
    "        return self.addDigits(total)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addDigits(self, num: int) -> int:\n",
    "        return (num - 1) % 9 + 1 if num else num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addDigits(self, num: int) -> int:\n",
    "        res = num\n",
    "        while res >= 10:\n",
    "            new_res = 0\n",
    "            res_str = str(res)\n",
    "            for c in res_str:\n",
    "                new_res += int(c)\n",
    "            res = new_res\n",
    "        return res\n",
    "\n",
    "\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addDigits(self, num: int) -> int:\n",
    "        if num == 0:\n",
    "            return 0\n",
    "        elif num % 9 != 0:\n",
    "            return num % 9\n",
    "        else:\n",
    "            return 9"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addDigits(self, num: int) -> int:\n",
    "        while num >= 10:\n",
    "            sum = 0\n",
    "            while num:\n",
    "                sum += num % 10\n",
    "                num //= 10\n",
    "            num = sum\n",
    "        return num\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def addDigits(self, num: int) -> int:\n",
    "        while num>9:\n",
    "            sum=0\n",
    "            while num:\n",
    "                sum+=num%10\n",
    "                num=num//10\n",
    "            num=sum\n",
    "        return num"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
